﻿using AutoMapper;
using BBICMS.Application.InputModels.Pages;
using BBICMS.Application.InputModels.Products;
using BBICMS.Application.ViewModels;
using BBICMS.Infrastructure.Configs;
using BBICMS.Model;
using BBICMS.Model.ViewModel.Back;
using BBICMS.Repository;
using BBICMS.Resources;
using BBICMS.ViewModel;
using BBICMS.WebUI.Filters;
using PagedList;
using SIG.Infrastructure.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BBICMS.WebUI.Areas.Admin.Controllers
{
    [SIGAuth]
    public class PageController : BaseController
    {
        // GET: Admin/Page
        public ActionResult Index(int? page)
        {
            PageListMV vm = new PageListMV();                     
          //  vm.Keyword = keyword; 
            vm.PageIndex = (page ?? 1) - 1;
            vm.PageSize = SettingsManager.Page.PageSize;
            ViewBag.PageSizes = new SelectList(Site.PageSizes());


            var defaultLang = LanguageHelper.GetDefaultCulture();
            var pls = _unit.PageLangs.GetPagedElements(vm.PageIndex, vm.PageSize, (p=>p.Page.Importance)  ,c => c.CultureName.Equals(defaultLang), false, c => c.Page).ToList();
            var pages = Mapper.Map<List<PageLang>, List<PageVM>>(pls);
          
            vm.TotalCount = _unit.Pages.GetAll().Count();
            vm.PageList = new StaticPagedList<PageVM>(pages, vm.PageIndex + 1, vm.PageSize, vm.TotalCount);

            return View(vm);
        }


        public ActionResult Add()
        {
            List<PageLangIM> pcl = Mapper.Map<List<Language>, List<PageLangIM>>(LanguageHelper.ActiveLanguages().ToList());

            var vPage = new PageIM
            {
                Active = true,
                Importance = 0,
                PageLangs = pcl
            };

            return View(vPage);
        }


        [HttpPost]

        public JsonResult Add(PageIM vm)
        {
            if (ModelState.IsValid)
            {
                var Page = new Page
                {
                    SeoName = vm.SeoName,
                    Active = vm.Active,
                    Importance = vm.Importance,
                    PageLangs = Mapper.Map<List<PageLangIM>, List<PageLang>>(vm.PageLangs)
                };

                _unit.Pages.Add(Page);
                _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.Page));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            AR.Setfailure(string.Format(Messages.AlertCreateFailure, EntityNames.Page));
            return Json(AR, JsonRequestBehavior.DenyGet);

        }
        public JsonResult IsExist(string SeoName,int id)
        {
            var path = SeoName.ToLower();
            var query = _unit.Pages.GetAll().Where(p => p.SeoName.Equals(path));

            if (id > 0)           
                query = query.Where(p=>p.Id != id);                
           
            var pages = query.Count();
            if (pages > 0)
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }          
          
            return Json(true, JsonRequestBehavior.AllowGet);           
        }

        public ActionResult Edit(int id)
        {
            var page = _unit.Pages.GetFirstOrDefault(c => c.Id == id, c => c.PageLangs);

            if (page != null)
            {
                PageIM Page = new PageIM
                {
                    SeoName = page.SeoName,                  
                    Active = page.Active,
                    Importance = page.Importance,
                    AddedBy = page.AddedBy,
                    AddedDate = page.AddedDate,
                    UpdatedBy = page.UpdatedBy,
                    UpdatedDate = page.UpdatedDate,
                    PageLangs = GetPageLangs(page.PageLangs)
                };
                //var Page = Mapper.Map<PageIM>(category);
                return View(Page);
            }
            return View("NotFound");
        }

        public List<PageLangIM> GetPageLangs(IEnumerable<PageLang> vm)
        {
            List<PageLangIM> result = new List<PageLangIM>();
            //  var pcl = vm.ToList();
            IEnumerable<PageLangIM> pcl = Mapper.Map<IEnumerable<Language>, IEnumerable<PageLangIM>>(LanguageHelper.ActiveLanguages());
            foreach (var cIM in pcl)
            {
                PageLangIM pclim = new PageLangIM();

                pclim.CultureName = cIM.CultureName;
                pclim.FullName = cIM.FullName;

                foreach (var v in vm)
                {
                    if (cIM.CultureName.Trim() == v.CultureName.Trim())
                    {
                        pclim.Title = v.Title;
                        pclim.Body = v.Body;
                        //  pclim.Description = v.Description;

                    }

                }
                result.Add(pclim);
            }
            return result;
        }

        [HttpPost]
        [ValidateInput(false)]

        public JsonResult Edit(PageIM vm)
        {

            if (ModelState.IsValid)
            {
                var page = _unit.Pages.GetById(vm.Id);

                //  var Page = Mapper.Map<Page>(PageIM);
                page.UpdatedDate = DateTime.Now;           
                page.Active = vm.Active;
                page.Importance = vm.Importance;
                page.SeoName = vm.SeoName;

                _unit.Pages.Update(page);

                var pclList = _unit.PageLangs.FindBy(l => l.PageId == page.Id);

                foreach (var pcl in vm.PageLangs)
                {
                    var pclIM = pclList.Where(l => l.CultureName == pcl.CultureName).FirstOrDefault();
                    if (pclIM != null)
                    {
                        pclIM.Title = pcl.Title;
                        pclIM.Body = pcl.Body;
                        _unit.PageLangs.Update(pclIM);
                    }
                    else
                    {
                        var vPCLIM = Mapper.Map<PageLangIM, PageLang>(pcl);
                        //  vPCLIM.CategoryId = vm.Id; 
                        vPCLIM.Page = page;
                        _unit.PageLangs.Add(vPCLIM);
                    }

                }




                _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.Page));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            AR.Setfailure(string.Format(Messages.AlertUpdateFailure, EntityNames.Page));
            return Json(AR, JsonRequestBehavior.DenyGet);

        }

        [HttpPost]
        public JsonResult IsActive(int id)
        {
            var product = _unit.Pages.GetById(id);
            if (product != null)
            {
                product.Active = !product.Active;
                _unit.Pages.Update(product);
                _unit.SaveChanges();

                AR.SetSuccess(Messages.AlertActionSuccess);
                return Json(AR, JsonRequestBehavior.DenyGet);

            }
            AR.Setfailure(Messages.AlertActionFailure);
            return Json(AR, JsonRequestBehavior.DenyGet);

        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Delete(int id)
        {

            Page vPage = _unit.Pages.GetById(id);
            if (vPage != null)
            {

                _unit.Pages.Delete(vPage);
                _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertDeleteSuccess, EntityNames.Page));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            AR.Setfailure(string.Format(Messages.AlertDeleteFailure, EntityNames.Page));
            return Json(AR, JsonRequestBehavior.DenyGet);
        }
    }
}